package com.lianzhihui.romeex.ui.me;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lianzhihui.romeex.R;
import com.lianzhihui.romeex.config.AppConstants;
import com.lianzhihui.romeex.ui.MainActivity;
import com.lianzhihui.romeex.ui.base.BaseActivity;
import com.lianzhihui.romeex.utils.LogUtil;
import com.lianzhihui.romeex.utils.NoDoubleClickUtils;
import com.lianzhihui.romeex.utils.PreferenceUtils;
import com.lianzhihui.romeex.utils.StatusBarUtil;
import com.lianzhihui.romeex.utils.UserUtils;
import com.lianzhihui.romeex.utils.fingersafe.BiometricPromptManager;
import com.lianzhihui.romeex.widget.OnOffView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ProjectName: romeex
 * @Package: com.lianzhihui.romeex.ui.me
 * @ClassName: BindPhoneActivity
 * @Description: java类作用描述
 * @Author: zhanghong
 * @CreateDate: 2020/11/2 15:50
 * @UpdateUser: 更新者：
 * @UpdateDate: 2020/11/2 15:50
 * @UpdateRemark: 更新说明：
 * @Version: 1.0
 */
public class SecurityCenterActivity extends BaseActivity {

    @BindView(R.id.include_top_iv_back)
    ImageView includeTopIvBack;
    @BindView(R.id.include_top_lin_newback)
    LinearLayout includeTopLinNewback;
    @BindView(R.id.include_top_tv_title)
    TextView includeTopTvTitle;
    @BindView(R.id.include_top_iv_right_second)
    ImageView includeTopIvRightSecond;
    @BindView(R.id.include_top_tv_right)
    TextView includeTopTvRight;
    @BindView(R.id.include_top_lin_right)
    LinearLayout includeTopLinRight;
    @BindView(R.id.include_top_lin_background)
    LinearLayout includeTopLinBackground;
    @BindView(R.id.app_top_bar)
    LinearLayout appTopBar;
    @BindView(R.id.tv_phone_number)
    TextView tvPhoneNumber;
    @BindView(R.id.onOffView)
    OnOffView onOffView;
    private boolean onErrorFinger = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_security_center;
    }

    @Override
    protected void initView() {
        StatusBarUtil.setTransparent(this);

        StatusBarUtil.setStatusBarTextColor(this, false);
        includeTopTvTitle.setText(getResources().getText(R.string.text_safe_center));
        boolean aBoolean = UserUtils.getCurrentFinger();
        onOffView.setDefOff(aBoolean);
        onOffView.setCheckBoxCall(isOff -> {
            if (!isOff) {
                //开启
                BiometricPromptManager mManager = BiometricPromptManager.from(this, true);
                if (mManager.isHardwareDetected()) {
                    //硬件支持
                    if (mManager.hasEnrolledFingerprints() && mManager.isKeyguardSecure()) {
                        //已经设置了指纹
                        if (mManager.isBiometricPromptEnable()) {
                            mManager.authenticate(new BiometricPromptManager.OnBiometricIdentifyCallback() {
                                @Override
                                public void onUsePassword() {
                                }

                                @Override
                                public void onSucceeded() {
                                    //指纹验证成功
                                    PreferenceUtils.applyBoolean(AppConstants.KEY_BIOMETRIC_SWITCH_ENABLE, true);
                                }

                                @Override
                                public void onFailed() {
                                    //指纹验证失败
//                                    Toast.makeText(SecurityCenterActivity.this, "onFailed", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onError(int code, String reason) {
                                    //指纹验证错误
                                    switch (code) {
                                        case 5:
                                            //指纹操作已取消。
                                            break;
                                        case 7:
                                            //尝试次数过多，请稍后重试。
                                            onErrorFinger = true;
                                            break;
                                        default:
                                            break;
                                    }
                                    toast(reason);
                                }

                                @Override
                                public void onCancel() {
                                    //指纹验证取消
//                                    Toast.makeText(SecurityCenterActivity.this, "onCancel", Toast.LENGTH_SHORT).show();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
//                                            onOffView.setDefOff(false);
                                            onOffView.onClick(onOffView);
                                        }
                                    });

                                }
                            });
                        }
                    } else {
                        //未设置指纹
                        Intent intent = new Intent();
                        String pcgName = "com.android.settings";
                        String clsName = "com.android.settings.fingerprint.FingerprintSettingsActivity";
                        ComponentName componentName = new ComponentName(pcgName, clsName);
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setComponent(componentName);
                        startActivity(intent);
                    }
                } else {
                    toast(getResources().getString(R.string.text_phone_not_support));
                }
            } else {
                //关闭
                PreferenceUtils.applyBoolean(AppConstants.KEY_BIOMETRIC_SWITCH_ENABLE, false);
            }
        });
    }

    @Override
    protected void initData() {

    }


    @OnClick({R.id.include_top_iv_back})
    public void OnClick(View v) {
        if (NoDoubleClickUtils.isDoubleClick()) {
            switch (v.getId()) {

                case R.id.include_top_iv_back:
                    finish();
                    break;

                default:
                    break;
            }
        }
    }

}
